package com.alipay.config;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Create By IntelliJ IDEA
 *
 * @author:lww
 * @create-Time: 2017/12/7 8:46
 * @description: ${DESCRIPTION}
 */
public class AlipayFactory {
    private static final String SIGN_TYPE="RSA2";
    private static final String CHARSET="utf-8";
    private static final String FORMAT="json";

    private static String appId;
    private static String merchantPrivateKey;
    private static String alipayPublicKey;
    private static String returnUrl;
    private static String gatewayUrl;

    static {
        try {
        InputStream stream = AlipayFactory.class.getClassLoader().getResourceAsStream("alipay.properties");
        Properties properties = new Properties();
        properties.load(stream);
        appId= (String) properties.get("app_id");
        merchantPrivateKey= (String) properties.get("merchant_private_key");
        alipayPublicKey= (String) properties.get("alipay_public_key");
        returnUrl= (String) properties.get("return_url");
        gatewayUrl= (String) properties.get("gatewayUrl");
        } catch (IOException e) {
            e.printStackTrace();
            throw new ExceptionInInitializerError(e);
        }
    }
    /**
     * 返回一个AlipayClient对象
     */
    public static AlipayClient client(){
        return new DefaultAlipayClient(gatewayUrl, appId,merchantPrivateKey, FORMAT, CHARSET, alipayPublicKey, SIGN_TYPE);
    }
    public static void setReturn(AlipayTradePagePayRequest request){
        request.setReturnUrl(returnUrl);
    }
}
